package com.yang.guliproduct.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.guliproduct.entity.PmsBrand;
import com.yang.guliproduct.service.impl.PmsBrandServiceImpl;
import com.yang.guliproduct.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 品牌 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/guliproduct/pms-brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandServiceImpl pmsBrandService;

    @PostMapping("getListPmsBrand/{current}/{limit}")
    public R getListPmsBrand(@PathVariable Long current, @PathVariable Long limit,
                             @RequestBody(required = false) PmsBrand pmsBrand){
        QueryWrapper<PmsBrand> allPmsBrand = pmsBrandService.getAllPmsBrand(pmsBrand);
        Page<PmsBrand> page = new Page<>(current, limit);
        pmsBrandService.page(page,allPmsBrand);
        long total = page.getTotal();
        List<PmsBrand> records = page.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    @PostMapping("addPmsBrand")
    public R addPmsBrand(@Valid @RequestBody PmsBrand pmsBrand, BindingResult result){
        if (result.hasErrors()){
            return R.error().code(400).message("提交数据不合法");
        }else {
            pmsBrandService.save(pmsBrand);
        }
        return R.ok();

    }

    @DeleteMapping("removePmsBrand/{brandId}")
    public R removePmsBrand(@PathVariable Long brandId){
        pmsBrandService.removeById(brandId);
        pmsBrandService.deleteCategoryBrand(brandId);
        return R.ok();
    }

    @PostMapping("updatePmsBrand")
    public R updatePmsBrand(@RequestBody PmsBrand pmsBrand){
        pmsBrandService.updateById(pmsBrand);
        pmsBrandService.updateCategoryBrand(pmsBrand);
        return R.ok();
    }

    @GetMapping("selectPmsBrand/{brandId}")
    public R selectPmsBrand(@PathVariable Long brandId){
        PmsBrand byId = pmsBrandService.getById(brandId);
        return R.ok().data("byId",byId);
    }

}

